﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BitcoinCommon;
using BitcoinExchange.Contract;
using BitcoinExchange.Data;
using BitcoinExchange.Market;
using BitcoinExchange.Wallet;

namespace BitcoinExchange
{
    public class Exchange
    {
        MainInfo _info;
        
        KorbitOrder _korbitOrder;
        KorbitContract _korbitContract;
        KorbitWallet _korbitWallet;

        BTCeOrder _BTCeOrder;
        BTCeContract _BTCeContract;
        BTCeWallet _BTCeWallet;

        public Exchange(MainInfo info)
        {
            _info = info;

            _korbitOrder = new KorbitOrder(info);
            _korbitContract = new KorbitContract(info);
            _korbitWallet = new KorbitWallet(info);

            _BTCeOrder = new BTCeOrder(info);
            _BTCeContract = new BTCeContract(info);
            _BTCeWallet = new BTCeWallet(info);
        }

        public string KorbitOrderBuy(string strBTCPrice, string strBTCVolume)
        {
            return OrderBuy(strBTCPrice, strBTCVolume, eMarketType.KORBIT);
        }

        public string KorbitOrderSell(string strBTCPrice, string strBTCVolume)
        {
            return OrderSell(strBTCPrice, strBTCVolume, eMarketType.KORBIT);
        }

        public string KorbitOrderCancel(string strOrderID)
        {
            return OrderCancel(strOrderID, eMarketType.KORBIT);
        }

        public string BTCeOrderBuy(string strBTCPrice, string strBTCVolume)
        {
            return OrderBuy(strBTCPrice, strBTCVolume, eMarketType.BTCE);
        }

        public string BTCeOrderSell(string strBTCPrice, string strBTCVolume)
        {
            return OrderSell(strBTCPrice, strBTCVolume, eMarketType.BTCE);
        }

        public string BTCeOrderCancel(string strOrderID)
        {
            return OrderCancel(strOrderID, eMarketType.BTCE);
        }

        public List<string> KorbitOpenOrderList()
        {
            return _korbitContract.OrderIDList();
        }
        
        public List<string> BTCeOpenOrderList()
        {
            return _BTCeContract.OrderIDList();
        }

        public Dictionary<string, string> KorbitOrderInfo(string strOrderID)
        {
            return _korbitContract.OrderInfo(strOrderID);
        }
        
        public Dictionary<string, string> BTCeOrderInfo(string strOrderID)
        {
            return _BTCeContract.OrderInfo(strOrderID);
        }


        public DataTable KorbitOpenOrderTable()
        {
            return OpenOrderTable(eMarketType.KORBIT).OpenOrderTable;
        }

        public DataTable BTCeOpenOrderTable()
        {
            return OpenOrderTable(eMarketType.BTCE).OpenOrderTable;
        }

        public double KRWAmount()
        {
            return _korbitWallet.KRWAmount;
        }

        public double USDAmount()
        {
            return _BTCeWallet.USDAmount;
        }

        public double KorbitBTCAmount()
        {
            return _korbitWallet.BTCAmount;
        }

        public double BTCeBTCAmount()
        {
            return _BTCeWallet.BTCAmount;
        }

        public void RetrievedOpenOrder()
        {
            _korbitContract.RetrivedOpenOrder();

            _BTCeContract.RetrivedOpenOrder();
        }

        public void RetrievedWallet()
        {
            _korbitWallet.RetrievedWallet();

            _BTCeWallet.RetrievedWallet();
        }

        private string OrderBuy(string strBTCPrice, string strBTCVolume, eMarketType eType)
        {
            string strReturn = "";

            if (eType == eMarketType.KORBIT)
                strReturn = _korbitOrder.OrderBuy(strBTCPrice, strBTCVolume);
            else
                strReturn = _BTCeOrder.OrderBuy(strBTCPrice, strBTCVolume);

            return strReturn;
        }

        private string OrderSell(string strBTCPrice, string strBTCVolume, eMarketType eType)
        {
            string strReturn = "";
            
            if (eType == eMarketType.KORBIT)
                strReturn = _korbitOrder.OrderSell(strBTCPrice, strBTCVolume);
            else
                strReturn = _BTCeOrder.OrderSell(strBTCPrice, strBTCVolume);

            return strReturn;
        }

        private string OrderCancel(string strOrderID, eMarketType eType)
        {
            string strReturn = "";

            if (eType == eMarketType.KORBIT)
                strReturn = _korbitOrder.OrderCancel(strOrderID);
            else
                strReturn = _BTCeOrder.OrderCancel(strOrderID);
            
            return strReturn;
        }

        private DataOpenOrder OpenOrderTable(eMarketType eType)
        {
            DataOpenOrder objData = null;

            if (eType == eMarketType.KORBIT)
                objData = _korbitContract.OpenOrderTable;
            else
                objData = _BTCeContract.OpenOrderTable;

            return objData;
        }

    }

    public enum eMarketType
    {
        KORBIT,
        BTCE
    }
}
